Private Sub mnuEditFormulas_Click()
Dim result%, jobnum%, mainjob%, formulacount%, FormulaN%, NameHandle&, NameLength%, textHandle&, TextLength%
Dim formulaName$, FormulaText$
result% = PEOpenEngine()
If result% = 0 Then
MsgBox "Could not start the report engine. Execution must halt.", vbOKOnly + vbCritical, "Serious Error"
End
End If
jobnum% = PEOpenPrintJob(lblReportName.Caption) ' Name from label on sample form
ErrorTrap "OpenPrintJob in EditFormulas", jobnum%
' Subreport check - if a subreport is currently selected on the main form, jobnum% becomes the subreport
If lblSubreportName.Visible Then
mainjob% = jobnum%
jobnum% = PEOpenSubreport(mainjob%, lblSubreportName.Caption)
ErrorTrap "OpenSubreport in EditFormulas", mainjob%
End If
' Count how many formulas there are
formulacount% = PEGetNFormulas(jobnum%)
If formulacount% =-1 Then
' Something went wrong
ErrorTrap "GetNFormulas in EditFormulas", jobnum%
ElseIf formulacount% = 0 Then
' None!
MsgBox "There are no formulas in this report.", vbOKOnly + vbCritical, "No Formulas"
Else
' There are formulas
' The formula display form is loaded so that the list box can be filled with formula names
Load Formula
CenterForm Sample, Formula
' Loop through the number of formulas determined by PEGetNFormulas
For FormulaN% = 0 To formulacount% - 1
' Get the handles and lengths for name and text
result% = PEGetNthFormula(jobnum%, FormulaN%, NameHandle&, NameLength%, textHandle&, TextLength%)
ErrorTrap "GetNthFormula in EditFormulas", jobnum%
' Set chr$(0) into the strings based on the lengths retrieved in the preceding call
' NOTE! Failure to do this step results in General Protection Faults
formulaName$ = String$(NameLength%, 0)
FormulaText$ = String$(TextLength%, 0)
' crvbHandleToBstr fills the strings
result% = crvbHandleToBstr(NameHandle&, formulaName$, NameLength%)
ErrorTrap "HandleToBstr on Name in EditFormulas", jobnum%
result% = crvbHandleToBstr(textHandle&, FormulaText$, TextLength%)
ErrorTrap "HandleToBstr on Text in EditFormulas", jobnum%
' The fieldname string is then inserted into the list box on the sort order form
Formula!lstNameList.AddItem formulaName$
Formula!lstTextList.AddItem FormulaText$
Next FormulaN%
' The formula list box and editor are in an infinite loop so that multiple formulas
' can be edited sequentially - pressing Ok or Cancel on the formula list form exits the loop
Do While True
Formula.Show 1 ' Modal
' Now that execution has returned, check what button was pressed and react accordingly
Select Case Formula.Tag
Case "Ok"
If MsgBox("Do you want to change the formulas of this print job?", vbYesNo + vbQuestion, "Change Formulas?") = vbYes Then
' Make changes to the print job formulas - note that this has no permanent affect on the report
' Changes made at the API level cannot be stored in the report
For FormulaN% = 0 To Formula!lstNameList.ListCount - 1
formulaName$ = Formula!lstNameList.List(FormulaN%) & Chr$(0)
FormulaText$ = Formula!lstTextList.List(FormulaN%) & Chr$(0)
' Set the formula
result% = PESetFormula(jobnum%, formulaName$, FormulaText$)
ErrorTrap "SetFormula in EditFormulas", jobnum%
Next FormulaN%
End If
Exit Do
Case "Edit"
' Although in theory we could use the data in the formula list boxes, for demonstration
' purposes, let's load the data for the formulas using the GetFormula call
' Get the name of the formula from the formula list box
formulaName$ = Formula!lstNameList.List(Formula!lstNameList.ListIndex)
' Get the handle and length for the formula text
result% = PEGetFormula(jobnum%, formulaName$, textHandle&, TextLength%)
ErrorTrap "GetFormula in EditFormulas", jobnum%
' Get the string of the formula text
FormulaText$ = String$(TextLength%, 0)
result% = crvbHandleToBstr(textHandle&, FormulaText$, TextLength%)
ErrorTrap "HandleToBstr for Text in EditFormulas", jobnum%
' Load the formula edit box with data and show it 1 ' Modally
Load FormulaEdit
CenterForm Sample, FormulaEdit
FormulaEdit.Caption = "Editing Formula: " & formulaName$
FormulaEdit!lblOriginalFormula.Caption = FormulaText$
FormulaEdit!txtEditFormula.Text = FormulaText$
' The formula edit procedure is placed in an infinite loop to allow for repeat editing and testing
Do While True
FormulaEdit.Show 1 ' Modal
' A button has been pressed on the formula edit form - find out which and act accordingly
Select Case FormulaEdit.Tag
Case "Ok"
' Replace the formula text in the list box with the edited formula text
Formula!lstTextList.List(Formula!lstTextList.ListIndex) = FormulaEdit!txtEditFormula.Text
Exit Do
Case "Test"
' In order to test the formula, you have to set it first
FormulaText$ = FormulaEdit!txtEditFormula.Text & Chr$(0)
result% = PESetFormula(jobnum%, formulaName$, FormulaText$)
ErrorTrap "SetFormula in EditFormulas", jobnum%
result% = PECheckFormula(jobnum%, formulaName$)
If result% = 0 Then
MsgBox "The formula has an error.", vbOKOnly + vbCritical, "Error!"
Else
MsgBox "The formula has no errors.", vbOKOnly + vbInformation, "No Error"
End If
' Now set the formula back to the original - the user can choose to keep the changes later
FormulaText$ = FormulaEdit!lblOriginalFormula.Caption & Chr$(0)
result% = PESetFormula(jobnum%, formulaName$, FormulaText$)
ErrorTrap "SetFormula in EditFormulas", jobnum%
Case "Cancel"
MsgBox "Cancel was pressed on the Formula edit form. The formula will not be changed.", vbOKOnly + vbInformation, "Cancel Pressed"
Exit Do
End Select
Loop
Unload FormulaEdit
Case "Cancel"
MsgBox "Cancel was pressed on the Formula form. The formulas of the print job will not be changed.", vbOKOnly + vbInformation, "Cancel Pressed"
Exit Do
End Select
Loop
' Finished with the sort order form - unload it
Unload Formula
' Offer opportunity to see what you did to the report
If MsgBox("Do you want to preview the report?", vbYesNo + vbQuestion, "Preview Report?") = vbYes Then
' Simplified version of the custom-l ink preview routine (no custom buttons)
result% = PEOutputToWindow(jobnum%, "Formula Demonstration Preview" & Chr$(0), CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0)
ErrorTrap "OutputtoWindow in EditFormulas", jobnum%
result% = PEStartPrintJob(jobnum%, True)
ErrorTrap "StartPrintJob in EditFormulas", jobnum%
result% = 1
Do While result% <> 0
DoEvents
DoEvents
result% = PEGetWindowHandle(jobnum%)
Loop
End If
End If
' Close print job and engine
' Subreport check - if a subreport is currently open, call PreviewReport to offer a chance to
' preview the main report, then close the subreport and main report
If lblSubreportName.Visible Then
PreviewReport mainjob%
result% = PECloseSubreport(jobnum%)
ErrorTrap "CloseSubreport in EditFormulas", mainjob%
PEClosePrintJob mainjob%
Else
PEClosePrintJob jobnum%
End If
PECloseEngine
MsgBox "Formula Edit Complete!", vbOKOnly, "Operation Succeeded"
End Sub
ActiveX
Sub mnuEditFormulas_Click()
Dim formulaN As Integer, GetFormulaN As String
Dim hwndPreviewWindow As Long
CrystalReport1.ReportFileName = lblReportName.Caption ' Name from label on sample form
' Set up endless loop (only ends with an Exit Do) for editing multiple formulas
Do While True
GetForumlaN = InputBox("Enter formula number to edit. Press Cancel to end editing of formulas:", "Enter Formula Number", Str$(formulaN))
' if a zero length string, then Cancel was pressed, exit the loop
If GetForumlaN = "" Then Exit Do
' Otherwise a formula number was entered, display a input box for the formula
formulaN = Val(GetForumlaN)
GetForumlaN = InputBox("Enter formula number " & formulaN & " using format 'FormulaName= Formula'. Do not put an @ at the front of the formula. All field names must be in braces. Press Cancel to abort editing of formula:", "Enter Formula", CrystalReport1.Formulas(formulaN))
If GetForumlaN = "" Then Exit Do
CrystalReport1.Formulas(formulaN) = GetForumlaN
formulaN = formulaN + 1
Loop
' Offer opportunity to see what you did to the report
If MsgBox("Do you want to preview the report?", vbYesNo + vbQuestion, "Preview Report?") = vbYes Then
CrystalReport1.Destination = 0 ' Window
CrystalReport1.Action = 1 ' Print
ErrorTrap "EditFormulas"
hwndPreviewWindow = GetActiveWindow()
Do While IsWindow(hwndPreviewWindow)
DoEvents
Loop
End If
' Close the report
CrystalReport1.ReportFileName = ""
MsgBox "Formula Edit Complete!", vbOKOnly, "Operation Completed"
End Sub
Seagate Software IMG Holdings, Inc. http://www.seagatesoftware.com Support services: http://support.seagatesoftware.com |